package com.test5.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test5.liang.dao.UserMapper;
import com.test5.liang.pojo.User;
import com.test5.liang.pojo.examination;
import com.test5.liang.service.UserService;
import com.test5.liang.service.examinationService;
import com.test5.liang.service.examtypeService;
import com.test5.liang.service.registerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test5.liang.pojo.registerLog;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class userController {

    @Autowired
    registerLogService registerLogService;

    @Autowired
    UserService userService;

    @Autowired
    examinationService examService;

    @Autowired
    examtypeService examtypeService;
    


    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 9:18 2021/12/13
     * @Param  查看用户信息
     * @return
     **/
    @RequestMapping("/userInfo")
    public String userInfo(@RequestParam("username")String username, Model model){
        User user = userService.findUserByName(username);
        model.addAttribute("user",user);
        System.out.println("****************");
        System.out.println(user.toString());
        String role;
        if (user.getUserRole()==1){
            role = "用户";
        }else {
            role = "管理员";
        }
        model.addAttribute("username",username);
        model.addAttribute("role",role);
        return "userInfo";
    }


      /**
      * @Author huangzhenliang
      * @Description //TODO 黄臻良
      * @Date 19:06 2021/12/14
      * @Param 修改个人信息
      * @return
      **/
      @RequestMapping("/updateuser")
      public String updateuser(User user, Model model){
          System.out.println("update----" + user.toString());
          User user1 = userService.findUserByName(user.getUserName());
          user.setUserId(user1.getUserId());
          userService.updateUser(user);
          return userInfo(user.getUserName(),model);
      }

    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 14:57 2021/12/19
     * @Param   查看用户历史测试成绩
     * @return
     **/
    @RequestMapping("/myExamHistory")
    public String myExamHistory(@RequestParam(value = "typeId",required = false)Integer typeId,
                                @RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                                Model model, HttpSession httpSession) {

        //获取所有的模式选项
        List<examination> examtypeList = examtypeService.selectAllExamtype();
        model.addAttribute("examtypeList",examtypeList);
        //获取userid
        String username = (String) httpSession.getAttribute("username");
        model.addAttribute("username",username);
        User user = userService.findUserByName(username);
        //获取该用户各种成绩记录
        examination examination = new examination();
        examination.setUserId(user.getUserId());
        if (typeId!=null){
            examination.setExamTypeId(typeId);
        }

        PageHelper.startPage(pagenum, 8);  //pagenum：页数，pagesize:每页的信息数
        List<examination> examinations = examService.selectExamination(examination);
        PageInfo<examination> pageInfo = new PageInfo<>(examinations); //得到分页结果对象
        model.addAttribute("examinations",examinations);
        model.addAttribute("pageInfo", pageInfo);  //携带分页结果信息

        //查询最近的7次成绩结果返回
        examination examination1 = new examination();
        examination1.setUserId(user.getUserId());
        List<examination> examinations1 = examService.selectExamination(examination1);
        ArrayList<Integer> list = new ArrayList<>();
        int index = Math.min(examinations1.size(), 7);
        for (int i=0;i<index;i++){
            list.add(examinations1.get(i).getExamScore());
        }
        //查询不同模式的次数
        List<examination> examinations2 = examService.selectExamtypeCount(examination);
        ArrayList<String> listname = new ArrayList<>();
        ArrayList<Integer> listvalue = new ArrayList<>();

        for (com.test5.liang.pojo.examination examination2 : examinations2) {
            listname.add(examination2.getExamTypename());
            listvalue.add(examination2.getTypenum());
        }
        model.addAttribute("listname",listname);
        model.addAttribute("listvalue",listvalue);

        model.addAttribute("ExamScore",list);
        return "userExamHistory";
    }






}